<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Ajax_mod extends CI_Model {

    private $_dbTableLogOnline = 'log_online';
    private $_dbTableUsers = 'users';
    private $_dbTableActivity = 'mod_activity';
	function __construct() 
	{
		// Call the Model constructor
        parent::__construct();
        $this->load->model('moderators_mod');
	}
	
    public function getNumOnline()
    {
        $sess_id = $this->session->userdata('session_id');
        $ip = $this->ipCheck();
        $user_id = $this->session->userdata('admin_id');
        $refresh_time = $this->config->item('refresh_time');
        
        $this->updateFakeUsersOnline();
        
        $time = time();        
        $date_time = date("Y-m-d H:i:s",$time);
        $select_time = date("Y-m-d H:i:s",$time - $refresh_time);
        $delete_time = date("Y-m-d H:i:s",$time - 2*$refresh_time);
        
        
        $query = $this->db->select('user_id')
                          ->from($this->_dbTableLogOnline)
                          //->where('session_id',$sess_id)
                          ->where('user_id',$user_id)
                          ->get();
        if( $query->num_rows()>0 )
        {
          $this->db->where('user_id',$user_id)
                   ->update($this->_dbTableLogOnline,array('time'=>$date_time /*, 'session_id'=>$sess_id */ ));
          //echo $this->db->last_query();
        }
        else
        {
          $this->db->insert($this->_dbTableLogOnline,
                            array(
                                //'session_id'=>$sess_id,
                                'ip' => $ip,
                                'user_id' => $user_id,
                                'time' => $date_time
                            )
                           );
          //echo $this->db->last_query();
        }
        $query->free_result();
        
        $this->db->query(" DELETE FROM {$this->_dbTableLogOnline} WHERE '{$delete_time}' > `time`; ");
        
        //$query = $this->db->query("SELECT DISTINCT `user_id` FROM {$this->_dbTableLogOnline} WHERE `time` > '{$select_time}'");
        $query = $this->db->query("SELECT DISTINCT {$this->_dbTableLogOnline}.`user_id` FROM {$this->_dbTableLogOnline},{$this->_dbTableUsers} WHERE {$this->_dbTableLogOnline}.`user_id` = {$this->_dbTableUsers}.`id` AND {$this->_dbTableUsers}.`user_group`=3 AND `time` > '{$select_time}'");
        //echo $this->db->last_query();
        return $query->num_rows();
    }
    
    public function updateFakeUsersOnline()
    {
      $user_id = $this->session->userdata('admin_id');
      
      $query = $this->db->select('fake_user_id')
                        ->from($this->_dbTableActivity)
                        ->where('mod_id',$user_id)
                        ->get();
      if( $query->num_rows()>0 )
      {
        foreach ( $query->result() as $row )
          $this->moderators_mod->setFakeUserOnline($row->fake_user_id);
      }
    }
    
    public function updateModActivity(){
      $refresh_time = $this->config->item('refresh_mod_activity_time');        
      $time = time();        
      $select_time = date("Y-m-d H:i:s",$time - $refresh_time);
      
      $query = $this->db->query("UPDATE {$this->_dbTableActivity} SET {$this->_dbTableActivity}.`active`=0 WHERE {$this->_dbTableActivity}.`mod_id` NOT IN (SELECT DISTINCT {$this->_dbTableLogOnline}.`user_id` FROM {$this->_dbTableLogOnline})");
      
    }




    /*
	This function will try to find out if user is coming behind proxy server. Why is this important?
	If you have high traffic web site, it might happen that you receive lot of traffic
	from the same proxy server (like AOL). In that case, the script would count them all as 1 user.
	This function tryes to get real IP address.
	Note that getenv() function doesn't work when PHP is running as ISAPI module
	*/
    private function ipCheck() {
	
		if (getenv('HTTP_CLIENT_IP')) {
			$ip = getenv('HTTP_CLIENT_IP');
		}
		elseif (getenv('HTTP_X_FORWARDED_FOR')) {
			$ip = getenv('HTTP_X_FORWARDED_FOR');
		}
		elseif (getenv('HTTP_X_FORWARDED')) {
			$ip = getenv('HTTP_X_FORWARDED');
		}
		elseif (getenv('HTTP_FORWARDED_FOR')) {
			$ip = getenv('HTTP_FORWARDED_FOR');
		}
		elseif (getenv('HTTP_FORWARDED')) {
			$ip = getenv('HTTP_FORWARDED');
		}
		else {
			$ip = $_SERVER['REMOTE_ADDR'];
		}
		return $ip;
	}
	
}

?>